package com.origin.niuke.dynamic;

/**
 * NC83 连续子数组的最大乘积
 * 算法：动态规划
 * max 为前 i 个数相乘的最大值
 * 因为存在负数相乘, 所以要记录最小值 min
 *
 * @author yezh
 * @version 1.0
 * @date 2022/10/27 20:51
 */
public class NC83 {

    public int maxProduct(int[] nums) {
        // write code here
        int ans = nums[0], max = nums[0], min = nums[0];
        for (int i = 1; i < nums.length; i++) {
            int a = max * nums[i], b = min * nums[i];
            max = Math.max(nums[i], Math.max(a, b));
            min = Math.min(nums[i], Math.min(a, b));
            ans = Math.max(ans, max);
        }
        return ans;
    }

}
